package mulMan.domain;

import java.util.Observable;
import java.util.Observer;

import mulMan.services.PlaylistServices;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class PlaylistModel extends Observable implements Observer, ApplicationContextAware{
	
	private ApplicationContext applicationContext;
	private PlaylistServices playlistServices;
	private Playlist playlist;
	private ListOfAudioFiles audioFilesModels;
	
	public PlaylistServices getPlaylistServices() {
		return playlistServices;
	}

	public void setPlaylistServices(PlaylistServices playlistServices) {
		this.playlistServices = playlistServices;
	}
	
	public String getName() {
		return playlist.getName();
	}
	
	public void setName(String name) {
		playlist.setName(name);
	}
	
	
	
	
	
	public PlaylistModel() {
		playlist = new Playlist();
		playlist.addObserver(this);
		audioFilesModels = new ListOfAudioFiles();
		audioFilesModels.addObserver(this);
	}
	
	
	
	

	public Playlist getPlaylist() {
		return playlist;
	}
	public void setPlaylist(Playlist playlist) {
		this.playlist = playlist;
		playlist.addObserver(this);
		
		audioFilesModels = new ListOfAudioFiles();
		
		for (AudioFile iterator : playlist.getAudioFiles()) {
			AudioFileModel audioFileModel = (AudioFileModel) applicationContext.getBean("audioFileModel");
			audioFileModel.setAudioFile(iterator);
			audioFilesModels.add(audioFileModel);
		}
		audioFilesModels.addObserver(this);
		
		setChanged();
		notifyObservers("all");
	}
	
	public ListOfAudioFiles getAudioFilesModels() {
		return audioFilesModels;
	}
	public void setAudioFilesModels(ListOfAudioFiles audioFilesModels) {
		playlist.getAudioFiles().clear();
		
		for (AudioFileModel iterator : audioFilesModels) {
			playlist.getAudioFiles().add(iterator.getAudioFile());
		}
		
		this.audioFilesModels = audioFilesModels;
		audioFilesModels.addObserver(this);
		
		setChanged();
		notifyObservers("list");
	}
	

	public void update(Observable arg0, Object arg1) {
		if ((arg0 instanceof ListOfAudioFiles) && (arg1 instanceof AudioFileModel)) {
			try {
				deleteAudioFileFromLibrary((AudioFileModel) arg1);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} 
		
		else {
			setChanged();
			notifyObservers(arg1);
		}
	}
	
	public String toString() {
		return playlist.toString();
	}
	
	public void addAudioFileToPlaylist(AudioFileModel audioFileModel) {
		playlist.getAudioFiles().add(audioFileModel.getAudioFile());
		audioFilesModels.add(audioFileModel);
		
		setChanged();
		notifyObservers("list");
	}
	
	

	
	public void updatePlaylist() throws Exception {
		playlistServices.updatePlaylist(playlist);
	}
	
	public void savePlaylist() throws Exception {
		playlistServices.savePlaylist(playlist);
	}
	
	public void deletePlaylist() throws Exception {
		playlistServices.deletePlaylist(playlist);
	}
	
	public void deleteAudioFileFromLibrary(AudioFileModel audioFileModel) throws Exception {
		setAudioFilesModels(audioFilesModels);
		updatePlaylist();
		
		if (this.getName().equals("Music")) {
			audioFileModel.deleteAudioFile();
		}
	}

	public void setApplicationContext(ApplicationContext arg0)
			throws BeansException {
		applicationContext = arg0;
	}
}
